package cn.com.blueInfo.bpm.number.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.number.entity.NumberSerial;
import cn.com.blueInfo.bpm.number.enums.NumberSerialResetCycle;
import cn.com.blueInfo.bpm.number.mapper.NumberSerialMapper;
import cn.com.blueInfo.bpm.number.repository.NumberSerialRepository;
import cn.com.blueInfo.bpm.number.request.NumberSerialDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 编号规则流水号表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberSerialRepositoryImpl
        extends ServiceImpl<NumberSerialMapper, NumberSerial>
        implements NumberSerialRepository, BaseRepository<NumberSerial, NumberSerialDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增编号规则流水号
     * @param numberSerial 编号规则流水号参数
     */
    @Override
    public Integer createNumberSerial(NumberSerial numberSerial) {
        return sqlExecuteResult(save(numberSerial));
    }

    /**
     * 批量新增编号规则流水号
     * @param numberSerialList 编号规则流水号参数列表
     */
    @Override
    public Integer batchCreateNumberSerial(List<NumberSerial> numberSerialList) {
        return sqlExecuteResult(saveBatch(numberSerialList), numberSerialList.size());
    }

    /**
     * 删除编号规则流水号
     * @param numberSerialId 编号规则流水号ID
     */
    @Override
    public Integer deleteNumberSerial(Long numberSerialId) {
        NumberSerial numberSerial = new NumberSerial();
        numberSerial.setId(numberSerialId);
        numberSerial.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(numberSerial));
    }

    /**
     * 批量删除编号规则流水号
     * @param numberSerialIdList 编号规则流水号ID列表
     */
    @Override
    public Integer batchDeleteNumberSerial(List<Long> numberSerialIdList) {
        List<NumberSerial> numberSerialList = new ArrayList<>();
        for (Long numberSerialId : numberSerialIdList) {
            NumberSerial numberSerial = new NumberSerial();
            numberSerial.setId(numberSerialId);
            numberSerial.setDeleted(BaseDeleted.DELETED);
            numberSerialList.add(numberSerial);
        }
        return sqlExecuteResult(removeBatchByIds(numberSerialList), numberSerialList.size());
    }

    /**
     * 更新部分编号规则流水号
     * @param numberSerial 编号规则流水号参数
     */
    @Override
    public Integer updateNumberSerialPartial(NumberSerial numberSerial) {
        validateUpdateId(numberSerial.getId());
        LambdaUpdateWrapper<NumberSerial> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NumberSerial::getId, numberSerial.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部编号规则流水号
     * @param numberSerial 编号规则流水号参数
     */
    @Override
    public Integer updateNumberSerial(NumberSerial numberSerial) {
        validateUpdateId(numberSerial.getId());
        return sqlExecuteResult(updateById(numberSerial));
    }

    /**
     * 查询一条编号规则流水号
     * @param numberSerialId 编号规则流水号ID
     */
    @Override
    public NumberSerial getNumberSerialById(Long numberSerialId) {
        LambdaQueryWrapper<NumberSerial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NumberSerial::getId, numberSerialId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询编号规则流水号
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public Page<NumberSerial> getNumberSerialPage(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        Page<NumberSerial> numberSerialPage = new Page<>(
                numberSerialQueryDTO.getPage(), numberSerialQueryDTO.getSize());
        LambdaQueryWrapper<NumberSerial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberSerialQueryDTO);

        BaseDeleted deleted = numberSerialQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(numberSerialPage, queryWrapper);
        } else {
            return page(numberSerialPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则流水号 数量
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public Long getNumberSerialCount(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        LambdaQueryWrapper<NumberSerial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberSerialQueryDTO);

        BaseDeleted deleted = numberSerialQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则流水号
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public List<NumberSerial> getNumberSerialList(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        LambdaQueryWrapper<NumberSerial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberSerialQueryDTO);

        BaseDeleted deleted = numberSerialQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<NumberSerial> queryWrapper,
                              NumberSerialDTO.QueryDTO numberSerialQueryDTO) {

        Long ruleId = numberSerialQueryDTO.getRuleId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(ruleId), NumberSerial::getRuleId, ruleId);

        Long orgId = numberSerialQueryDTO.getOrgId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orgId), NumberSerial::getOrgId, orgId);

        Long currentNum = numberSerialQueryDTO.getCurrentNum();
        queryWrapper.eq(ObjectUtils.isNotEmpty(currentNum), NumberSerial::getCurrentNum, currentNum);

        Long minNum = numberSerialQueryDTO.getMinNum();
        queryWrapper.eq(ObjectUtils.isNotEmpty(minNum), NumberSerial::getMinNum, minNum);

        Long maxNum = numberSerialQueryDTO.getMaxNum();
        queryWrapper.eq(ObjectUtils.isNotEmpty(maxNum), NumberSerial::getMaxNum, maxNum);

        NumberSerialResetCycle resetCycle = numberSerialQueryDTO.getResetCycle();
        queryWrapper.eq(ObjectUtils.isNotEmpty(resetCycle), NumberSerial::getResetCycle, resetCycle);

        LocalDateTime lastResetTime = numberSerialQueryDTO.getLastResetTime();
        queryWrapper.eq(ObjectUtils.isNotEmpty(lastResetTime), NumberSerial::getLastResetTime, lastResetTime);

        String format = numberSerialQueryDTO.getFormat();
        queryWrapper.eq(StringUtils.isNotBlank(format), NumberSerial::getFormat, format);

        addQueryWrapper(queryWrapper, numberSerialQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<NumberSerial, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                NumberSerial::getOrderNumber,
                NumberSerial::getTenantId,
                NumberSerial::getApplicationId,
                NumberSerial::getStatus,
                NumberSerial::getDeleted,
                NumberSerial::getDeleteUserId,
                NumberSerial::getDeleteTime,
                NumberSerial::getRemark,
                NumberSerial::getCreateUserId,
                NumberSerial::getCreateTime,
                NumberSerial::getUpdateUserId,
                NumberSerial::getUpdateTime,
                NumberSerial::getExtend1,
                NumberSerial::getExtend2,
                NumberSerial::getExtend3);
    }

}
